package com.lhz.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.lhz.model.response.ResponseObject;
import com.lhz.model.form.JobInfoForm;
import com.lhz.model.param.JobInfoParam;
import com.lhz.model.vo.JobInfoVo;
import com.lhz.service.JobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @Author LiGezZ
 * @Date 2021/08/02 20:56:46
 * @Description: 定时任务调度
 */

@Tag(name = "任务管理")
@ApiSort(10)
@RestController
@RequestMapping("/info")
public class JobController {

    @Resource
    private JobService jobService;


    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据Id查询", description = "根据Id查询")
    @ApiResponse(content = @Content(schema = @Schema(implementation = JobInfoVo.class)))
    @ApiOperationSupport(order = 5)
    @GetMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "Long"), required = true)
    })
    public ResponseObject selectByPrimaryKey(@PathVariable("id") Long id) {
        Object obj = jobService.selectByPrimaryKey(id);
        return ResponseObject.success(obj);
    }

    /**
     * 查询定时任务调度列表
     *
     * @param param
     * @return
     */
    @Operation(summary = "列表查询", description = "列表查询")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = JobInfoVo.class))))
    @ApiOperationSupport(order = 10)
    @GetMapping
    public ResponseObject listInfoPage(JobInfoParam param) {
        Object obj = jobService.listInfoPage(param);
        return ResponseObject.success(obj);
    }

    /**
     * 新增定时任务调度
     *
     * @param form
     * @return
     */
    @Operation(summary = "新增数据", description = "新增数据")
    @ApiResponse(content = @Content(schema = @Schema(implementation = ResponseObject.class)))
    @ApiOperationSupport(order = 15)
    @PostMapping

    public ResponseObject insert(@RequestBody JobInfoForm form) {
        int num = jobService.insert(form);
        return ResponseObject.successPost(num);
    }

    /**
     * 修改定时任务调度
     *
     * @param form
     * @return
     */
    @Operation(summary = "修改数据", description = "修改数据")
    @ApiResponse(content = @Content(schema = @Schema(implementation = ResponseObject.class)))
    @ApiOperationSupport(order = 20)
    @PutMapping
    public ResponseObject updateByPrimaryKey(@RequestBody JobInfoForm form) {
        int num = jobService.updateByPrimaryKey(form);
        return ResponseObject.successUpdate(num);
    }


    /**
     * 删除定时任务调度
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除数据", description = "删除数据")
    @ApiResponse(content = @Content(schema = @Schema(implementation = ResponseObject.class)))
    @ApiOperationSupport(order = 25)
    @DeleteMapping("single/{id}")
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "Long"), required = true)
    })
    public ResponseObject deleteByPrimaryKey(@PathVariable("id") Long id) {
        int num = jobService.deleteByPrimaryKey(id);
        return ResponseObject.successDel(num);
    }

    /**
     * 单次执行任务
     *
     * @param id
     * @return
     */
    @Operation(summary = "单次执行任务", description = "单次执行任务")
    @ApiResponse(content = @Content(schema = @Schema(implementation = ResponseObject.class)))
    @ApiOperationSupport(order = 30)
    @PutMapping("invoke/{id}")
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "Long"), required = true)
    })
    public ResponseObject invokeJob(@PathVariable("id") Long id) {
        jobService.invokeJob(id);
        return ResponseObject.success();
    }

    /**
     * 启动任务
     *
     * @param id
     * @return
     */
    @Operation(summary = "启动任务", description = "启动任务")
    @ApiResponse(content = @Content(schema = @Schema(implementation = ResponseObject.class)))
    @ApiOperationSupport(order = 35)
    @PutMapping("start/{id}")
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "Long"), required = true)
    })
    public ResponseObject startJob(@PathVariable("id") Long id) {
        jobService.startJob(id);
        return ResponseObject.success();
    }

    /**
     * 暂停任务
     *
     * @param id
     * @return
     */
    @Operation(summary = "暂停任务", description = "暂停任务")
    @ApiResponse(content = @Content(schema = @Schema(implementation = ResponseObject.class)))
    @ApiOperationSupport(order = 40)
    @PutMapping("stop/{id}")
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "Long"), required = true)
    })
    public ResponseObject stopJob(@PathVariable("id") Long id) {
        jobService.stopJob(id);
        return ResponseObject.success();
    }


    /**
     * 禁用任务
     *
     * @param id
     * @return
     */
    @Operation(summary = "禁用任务", description = "禁用任务")
    @ApiOperationSupport(order = 45)
    @PutMapping("disable/{id}")
    public ResponseObject disableJob(@PathVariable("id") Long id) {
        jobService.disableJob(id);
        return ResponseObject.success();
    }
}
